<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :title="`${dialogProps.title}类型`" width="450px">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="编码" prop="customeCode">
        <el-input v-model="dialogProps.row!.customeCode" placeholder="请填写" clearable />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dialogProps.row!.name" placeholder="请填写" clearable />
      </el-form-item>
      <el-form-item label="通用类型" prop="commTypeId">
        <el-select v-model="dialogProps.row!.commTypeId " placeholder="请选择">
          <el-option v-for="item in dialogProps.typeList" :key="item.value" :label="item.description" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="助记码" prop="alias">
        <el-input disabled v-model="dialogProps.row!.alias" placeholder="请填写" clearable />
      </el-form-item>
      <!-- <el-form-item label=类型状态" prop="status">
        <el-radio-group v-model="dialogProps.row!.status">
          <el-radio v-for="item in generalStatus" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input v-model="dialogProps.row!.contact" placeholder="请填写" clearable />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dialogProps.row!.mobile" placeholder="请填写" clearable />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dialogProps.row!.address" placeholder="请填写" clearable />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model="dialogProps.row!.tag" placeholder="请填写" clearable />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="memberDrawer">
import { ref, reactive } from "vue";
import { generalStatus } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { Member } from "@/api/interface";

const rules = reactive({
  name: [{ required: true, message: "请填写类型名称" }],
  customeCode: [{ required: true, message: "请填写类型编码" }],
  commTypeId: [{ required: true, message: "请选择通用类型" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Member.ResMemberGradeList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  typeList: { description: string; title: string; value: number }[];
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {},
  typeList: []
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.title}类型成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
